Imports System.Data
Imports System.Data.SqlClient
Imports System.Reflection
Imports System.Xml.Serialization

'Namespace SVCMS.Library

<Serializable()> Public MustInherit Class DBTableObjectBase
    Implements IDBTableObject, IDisposable

#Region "Declarations"
    Protected Friend iId As Guid '= Guid.NewGuid
    Protected Friend iLMUserId As Guid
    Protected Friend iLMDate As DateTime = DateTime.Now
    'Protected Friend iChanged As TableObjectChangeTypesEnum = TableObjectChangeTypesEnum.NewItem
    'Protected Friend iReturnMessage As String = ""
    <NonSerialized()> Protected Friend iConnection As SqlClient.SqlConnection
    <NonSerialized()> Protected Friend iTransaction As SqlClient.SqlTransaction
    Protected Friend iTableName As TableNamesEnum
#End Region

#Region "Constructors"
    Private Sub New()
    End Sub
    Friend Sub New(ByVal TableName As TableNamesEnum)
        Me.iTableName = TableName
        'Me.iChanged = TableObjectChangeTypesEnum.NewItem
    End Sub
    Friend Sub New(ByVal TableName As TableNamesEnum, ByVal Id As Guid)
        Me.iId = Id
        iTableName = TableName
        'Me.iChanged = TableObjectChangeTypesEnum.NewItem
    End Sub
#End Region

#Region "Properties"
    <XmlIgnore()> Protected Friend Property Connection() As SqlConnection
        Get
            Return Me.iConnection
        End Get
        Set(ByVal value As SqlConnection)
            Me.iConnection = value
        End Set
    End Property
    <XmlIgnore()> Protected Friend Property Transaction() As SqlTransaction
        Get
            Return Me.iTransaction
        End Get
        Set(ByVal value As SqlTransaction)
            Me.iTransaction = value
        End Set
    End Property
    Public Overridable Property LMUserId() As Guid
        Get
            Return Me.iLMUserId
        End Get
        Set(ByVal value As Guid)
            Me.iLMUserId = value
        End Set
    End Property
    Public Overridable Property LMDate() As DateTime
        Get
            Return Me.iLMDate
        End Get
        Set(ByVal value As DateTime)
            Me.iLMDate = value
        End Set
    End Property
#End Region

#Region "Procedures.."
    Protected Friend MustOverride Function Load() As Boolean Implements IDBTableObject.Load
    Protected Friend MustOverride Function Insert() As Boolean Implements IDBTableObject.Insert
    Protected Friend MustOverride Function Update() As Boolean Implements IDBTableObject.Update
    Protected Friend Overridable Function Delete() As Boolean Implements IDBTableObject.Delete
        'sp_[TableName]Delete
        Dim resBool As Boolean = True
        Dim resInt As Integer
        Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand("sp_" & iTableName.ToString & "Delete '" & Me.iId.ToString & "'", Me.iConnection)
        If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
        Try
            resInt = cmd.ExecuteNonQuery()
        Catch ex As Exception
            resBool = False
            Throw ex
        End Try
        If resInt = 0 Then
            Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
        End If

        Return resBool
    End Function
    Protected Friend MustOverride Function ValidateOnInsert() As Boolean Implements IDBTableObject.ValidateOnInsert
    Protected Friend MustOverride Function ValidateOnUpdate() As Boolean Implements IDBTableObject.ValidateOnUpdate
    Protected Friend MustOverride Function ValidateOnDelete() As Boolean Implements IDBTableObject.ValidateOnDelete
    Public MustOverride Overrides Function ToString() As String
    Public MustOverride Function ToDetailsListBoxItem() As Abyx.WindowsExtendedControls.DetailListBoxItem
#End Region

#Region "Overloads"
    Public Overloads Shared Operator =(ByVal obj1 As DBTableObjectBase, ByVal obj2 As DBTableObjectBase) As Boolean
        Return (obj1.iId.Equals(obj2.iId))
    End Operator
    Public Overloads Shared Operator <>(ByVal obj1 As DBTableObjectBase, ByVal obj2 As DBTableObjectBase) As Boolean
        Return (Not (obj1.iId.Equals(obj2.iId)))
    End Operator
    Public Overloads Shared Operator &(ByVal obj1 As DBTableObjectBase, ByVal obj2 As DBTableObjectBase) As String
        Return (obj1.ToString & obj2.ToString)
    End Operator
    'Public Function IIf(ByVal expression As Boolean, ByVal truePart As Object, ByVal falsePart As Object) As Object
    '    If expression Then
    '        Return truePart
    '    Else
    '        Return falsePart
    '    End If
    'End Function
#End Region

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

'End Namespace
